//
//  SCPageScrollView.m
//  LastOne
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "SCPageScrollView.h"
#import <UIImageView+WebCache.h>
@interface SCPageScrollView ()<UIScrollViewDelegate>
@property(nonatomic, strong)NSTimer *timer;
@property(nonatomic, assign)BOOL flag;
@end

@implementation SCPageScrollView

-(id)initWithFrame:(CGRect)frame PicArray:(NSArray *)array
{
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        
        //无图的出错处理
        if (array.count == 0) {
            return self;
        }
        //数组初始化
        self.picArray = [NSMutableArray array];
        [_picArray addObject:[array lastObject]];
        [_picArray addObjectsFromArray:array];
        [_picArray addObject:[array firstObject]];
        
        //scrollView初始化
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 200)];
        _scrollView.contentSize = CGSizeMake(frame.size.width * _picArray.count, frame.size.height);
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.contentOffset = CGPointMake(frame.size.width, 0);
        for (NSInteger i = 0; i < _picArray.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width * i, 0, frame.size.width, frame.size.height)];
            imageView.tag = 2000 + i;
            imageView.userInteractionEnabled = YES;
            [imageView sd_setImageWithURL:[NSURL URLWithString:_picArray[i]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            [imageView addGestureRecognizer:tap];
            [_scrollView addSubview:imageView];
        }
        [self addSubview:_scrollView];
        
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(20, 100, 100, 30)];
        _pageControl.numberOfPages = array.count;
        [self addSubview:_pageControl];
        
        _timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    }
    return self;
}

-(void)changeImage
{
    if (_flag) {
        _flag = NO;
        return;
    }
    if (_scrollView.contentOffset.x <= self.frame.size.width * (_picArray.count - 3)) {
        //[UIView animateWithDuration:1.0 animations:^{
            _scrollView.contentOffset = CGPointMake(self.frame.size.width + _scrollView.contentOffset.x, 0);
       // }];
        _pageControl.currentPage = (NSInteger)((_scrollView.contentOffset.x + 1) / self.frame.size.width) - 1;
    }
    else{
       // [UIView animateWithDuration:1.0 animations:^{
            _scrollView.contentOffset = CGPointMake(self.frame.size.width + _scrollView.contentOffset.x, 0);
      //  }];
        _pageControl.currentPage = 0;
        _scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _flag = YES;
    if (scrollView.contentOffset.x == 0) {
        _scrollView.contentOffset = CGPointMake(scrollView.frame.size.width * (_picArray.count - 2), 0);
    }
    else if ((NSInteger)((scrollView.contentOffset.x + 1) / scrollView.frame.size.width) == _picArray.count - 1){
        _scrollView.contentOffset = CGPointMake(scrollView.frame.size.width, 0);
    }
    //+1是因为小数计算的问题
    _pageControl.currentPage = (NSInteger)((scrollView.contentOffset.x + 1) / scrollView.frame.size.width) - 1;
}


-(void)tapAction:(UITapGestureRecognizer *)sender
{
    [self.delegate SCScrollerViewDidClicked:sender.view.tag - 2000];
}

@end
